package com.sh.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sh.common.BaseResult;
import com.sh.common.enums.BizCodeEnum;
import com.sh.common.enums.PublishedEnum;
import com.sh.common.pojo.Contants;
import com.sh.common.pojo.bo.AlgoSaveBO;
import com.sh.common.pojo.bo.PutObjectRes;
import com.sh.common.pojo.dto.AlgoDto;
import com.sh.common.pojo.query.AlgoPageQuery;
import com.sh.common.pojo.query.AlgoParseParam;
import com.sh.common.utils.FileUtil;
import com.sh.entity.Algo;
import com.sh.entity.AlgoDetail;
import com.sh.exceptions.BizException;
import com.sh.service.IAlgoDetailService;
import com.sh.service.IAlgoService;
import com.sh.service.IOSSService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/algo")
public class AlgoController {

    @Autowired
    private IAlgoService algoService;

    @Autowired
    private IAlgoDetailService algoDetailService;

    @Autowired
    private IOSSService ossService;


    @PostMapping("/save")
    @Transactional
    public BaseResult save(@Valid @RequestBody AlgoSaveBO saveBO) throws BizException {
        Algo algo;
        if (saveBO.getId() != null) {
            algo = algoService.getById(saveBO.getId());
            if (algo == null) {
                return BaseResult.fail(BizCodeEnum.ALGO_NOT_EXISTED);
            }
        } else {
            algo = new Algo();
        }

        BeanUtil.copyProperties(saveBO, algo);
        Date now = new Date();
        algo.setUpdateTime(now);
        algoService.saveOrUpdate(algo);

        QueryWrapper<AlgoDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("algo_id", algo.getId());
        AlgoDetail algoDetailDB = algoDetailService.getOne(queryWrapper);
        if (algoDetailDB == null) {
            algoDetailDB = new AlgoDetail();
            algoDetailDB.setAlgoId(algo.getId());
        }
        if (StringUtils.isNotBlank(saveBO.getAlgoSchema())) {
            algoDetailDB.setAlgoSchema(saveBO.getAlgoSchema());
            String objectName = FileUtil.generateAlgoSchemaOssKey(algo.getId());
            PutObjectRes uploadRet = ossService.putObject(objectName, saveBO.getAlgoSchema());
            if (uploadRet.getSuccess()) {
                algoDetailDB.setAlgoSchemaPath(Contants.OSS_PREFIX + objectName);
            } else {
                throw new BizException(BizCodeEnum.ALGO_SAVE_ERROR);
            }
        }
        if (CollectionUtil.isNotEmpty(saveBO.getOperatorParamList())) {
            algoDetailDB.setOperatorParam(JSON.toJSONString(saveBO.getOperatorParamList()));
        }
        if (CollectionUtil.isNotEmpty(saveBO.getGlobalParamList())) {
            algoDetailDB.setGlobalParam(JSON.toJSONString(saveBO.getGlobalParamList()));
        }
        if (CollectionUtil.isNotEmpty(saveBO.getOutputParamList())) {
            algoDetailDB.setGlobalParam(JSON.toJSONString(saveBO.getGlobalParamList()));
        }
        algoDetailService.saveOrUpdate(algoDetailDB);
        return BaseResult.success(algo);
    }

}
